package com.genius.reservation.receiveManager.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genius.reservation.UseTime.entity.UseTimeEntity;
import com.genius.reservation.UseTime.service.IUseTimeService;
import com.genius.reservation.car.entity.ReservationStartCarInformation;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.common.constant.Constant;
import com.genius.reservation.common.constant.ResStatusCode;
import com.genius.reservation.common.rabbitmq.RabbitMessageEntity;
import com.genius.reservation.common.rabbitmq.RabbitService;
import com.genius.reservation.mapper.ReceiveManagerInformationMapper;
import com.genius.reservation.mapper.ReceiveRecordInformationMapper;
import com.genius.reservation.mapper.ReservationInformationMapper;
import com.genius.reservation.mapper.ReservationStartCarInformationMapper;
import com.genius.reservation.platform.entity.PlatformInformation;
import com.genius.reservation.platform.service.IPlatformService;
import com.genius.reservation.receiveManager.entity.ReceiveManagerInformation;
import com.genius.reservation.receiveManager.service.IReceiveManagerInformationService;
import com.genius.reservation.receiveRecord.entity.ReceiveRecordInformation;
import com.genius.reservation.receiver.entity.ReceiverInformation;
import com.genius.reservation.receiver.service.IReceiverInformationService;
import com.genius.reservation.reservation.entity.ReservationInformation;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.user.entity.UserInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收货主管 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-05-26
 */
@Service
public class ReceiveManagerInformationServiceImpl extends ServiceImpl<ReceiveManagerInformationMapper, ReceiveManagerInformation> implements IReceiveManagerInformationService {
    @Autowired
    IReservationInformationService reservationService;
    @Autowired
    RabbitService rabbitService;

    @Autowired
    IPlatformService platformService;

    @Autowired
    IReceiverInformationService receiverService;
    @Autowired
    @Lazy
    ReservationStartCarInformationMapper startCarMapper;

    @Autowired
    @Lazy
    ReservationInformationMapper reservationMapper;

    @Autowired
    @Lazy
    ReceiveRecordInformationMapper recordMapper;

    @Autowired
    @Lazy
    IUseTimeService useTimeService;


    public static Map<String, RabbitMessageEntity> message = new HashMap<>();

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReceiveManagerInformation createReceiveManager(UserInformation user) {
        ReceiveManagerInformation receiveManagerInformation = new ReceiveManagerInformation();
        receiveManagerInformation.setReceiveManagerNumber(Constant.RECEIVE_MANAGER_PREFIX + IdUtil.randomUUID());
        receiveManagerInformation.setReceiveManagerName(user.getName());
        return this.saAndUp(receiveManagerInformation);
    }

    @Override
    public void newMessage(RabbitMessageEntity obj) {
        synchronized (this) {
            System.out.println(obj);
            message.put(obj.getReservationNumber(),obj);
        }
    }

    @Override
    public ResponseRule getMessage(String receiveManagerNumber) {
        synchronized (this) {
            ResponseRule<Object> ok;
            if (message.size() != 0) {
                ok = ResponseRule.ok(message.get(receiveManagerNumber).getData());
                message.remove(receiveManagerNumber);
            } else {
                ok = ResponseRule.ok("没有新消息");
            }
            return ok;
        }
    }

    @Override
    public ResponseRule getAllMessage() {
        synchronized (this) {
            ResponseRule<Object> ok;
            if (message.size() != 0) {
                ok = ResponseRule.ok(message.values());
                message =  new HashMap<>();
            } else {
                ok = ResponseRule.ok("没有新消息");
            }
            return ok;
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseRule changePlatformAndReceiver(String reservationNumber, List<JSONObject> cars) {
        ReservationInformation reservationInformation = reservationService.getByReservationNumber(reservationNumber);
        reservationInformation.setIsReassign(1);
        ReservationInformation reservationInformation1 = reservationService.addAndUp(reservationInformation);
        for (JSONObject car : cars) {
            ReservationStartCarInformation carInformation = startCarMapper.selectById(car.getString("carId"));
            if(ObjectUtil.isEmpty(carInformation))
                return ResponseRule.no("没有找到车辆信息");
            carInformation.setActualReceiverNumber(car.getString("receiver"));
            carInformation.setActualPlatformNumber(car.getString("platform"));
            carInformation.setIsReassign(1);//代表是重新分配的
            ReservationStartCarInformation up = startCarMapper.up(carInformation);

            //修改usetime表数据
            //TODO: 发车记录里面缺少 占用时间的ID
            LambdaQueryWrapper<UseTimeEntity> receiver = new LambdaQueryWrapper<UseTimeEntity>().eq(UseTimeEntity::getId, carInformation.getUseTimeId());
            UseTimeEntity one = useTimeService.getOne(receiver);
            if(ObjectUtil.isEmpty(one)) return ResponseRule.no("未找到此时段占用信息");
            one.setReceiverNumber(car.getString("receiver"));
            one.setPlatformNumber(car.getString("platform"));
            useTimeService.addAndUp(one);

            RabbitMessageEntity<ReservationInformation> obj = new RabbitMessageEntity<>();
            obj.setMsg(String.format("月台或收货员信息已修改,请及时查看前往指定月台装卸,单号为:%s",up.getReservationNumber()));
            obj.setData(reservationService.getByReservationNumber(up.getReservationNumber()));
            rabbitService.ToDriver(obj);
            rabbitService.ToReceiver(obj);
        }
        return ResponseRule.ok(reservationInformation1,"重新分配成功");
    }

    @Override
    public ResponseRule getUncompletedReservation() {
        return reservationService.getUncompletedReservation();
    }

    @Override
    public ResponseRule getCompleteReservation() {
        return reservationService.getCompleteReservation();
    }

    @Override
    public ResponseRule getLateReservation() {
        return reservationService.getLateReservation();
    }

    @Override
    public ResponseRule getUnlateReservation() {
        return reservationService.getUnlateReservation();
    }

    @Override
    public ResponseRule getPlatformById(String platformNumber) {
        PlatformInformation platformById = platformService.findPlatformById(platformNumber);
        return ResponseRule.ok(platformById);
    }

    @Override
    public ResponseRule getConfirmReservation() {
        List<ReservationInformation> confirmReservation = reservationMapper.getConfirmReservation();
        if (ObjectUtil.isEmpty(confirmReservation)) {
            return ResponseRule.no("未找到");
        }
        return ResponseRule.ok(confirmReservation);
    }

    @Override
    public ResponseRule getReceivingReservation() {

        List<ReceiveRecordInformation> receivingReservation = recordMapper.getReceivingReservation();
        if (ObjectUtil.isEmpty(receivingReservation))
            return ResponseRule.no("未找到");
        return ResponseRule.ok(receivingReservation);
    }

    @Override
    public ResponseRule getReceivedReservation() {
        List<ReceiveRecordInformation> receivedReservation = recordMapper.getReceivedReservation();
        if (ObjectUtil.isEmpty(receivedReservation)) {
            return ResponseRule.no("未找到");
        }
        return ResponseRule.ok(receivedReservation);
    }

    @Override
    public ResponseRule getReceiveErr() {
        LambdaQueryWrapper<ReceiveRecordInformation> eq = recordMapper.getWrapper().eq(ReceiveRecordInformation::getReceiveStatus, 2);
        List<ReceiveRecordInformation> receiveRecordInformations = recordMapper.selectList(eq);
        if (ObjectUtil.isEmpty(receiveRecordInformations)) {
            return ResponseRule.no("未找到");
        }
        return ResponseRule.ok(receiveRecordInformations);
    }

    @Override
    public ResponseRule getReservationErr() {
        LambdaQueryWrapper<ReservationStartCarInformation> eq = startCarMapper.getWrapper().eq(ReservationStartCarInformation::getNowStatus, ResStatusCode.RESERVATION_EXCEPTION);
        List<ReservationStartCarInformation> reservationStartCarInformations = startCarMapper.selectList(eq);
        if (ObjectUtil.isEmpty(reservationStartCarInformations)) {
            return ResponseRule.no("未找到");
        }
        return ResponseRule.ok(reservationStartCarInformations);
    }

    @Override
    public ResponseRule lateEarlyRelease(int startCarId) {
        ReservationStartCarInformation carInformation = startCarMapper.selectById(startCarId);
        if(ObjectUtil.isEmpty(carInformation)) return ResponseRule.no("没有找到发车单");
//        carInformation
        return null;
    }

}
